<!-- Wiki text -->
<div class="wiki_text" id="wiki_text_for_<?php echo $inst->id; ?>">
	<?php echo $first_ver->value; ?>
</div>

<!-- Wiki controls -->
<?php if ($this->userland->logged_in()): ?>
<div class="block_header">
	<?php
	if ($versions):
	 	$i = $versions->num_rows();
	?>
	<div id="wiki_wrap_version_<?php echo $inst->id; ?>" style="display: none; margin-bottom: 3px;">
		<label for="wiki_version_<?php echo $inst->id; ?>">Show version</label>
		<select id="wiki_version_<?php echo $inst->id; ?>"
			onchange="Wiki.updateWiki(<?php echo $inst->id; ?>, <?php echo $content->id; ?>); $('#wiki_wrap_version_<?php echo $inst->id; ?>').hide();">
			<?php foreach ($versions->result() as $version): ?>
				<?php
				// load the user for this comment and cache them for future use
				if (isset($WIKI_USERS[$version->user_id]))
					$cmt_user = $WIKI_USERS[$version->user_id];
				else
				{
					$query 	  = $this->db->query('select * from users where id = ? limit 1', $version->user_id);
					$w_user   = $query->first_row();
					$WIKI_USERS[$version->user_id] = $w_user;
				}
				?>
			<option <?php if ($version->id == $first_ver->id) echo 'selected="selected"'; ?> value="<?php echo $version->id; ?>"><?php echo $i; ?> by <?php echo username($w_user); ?> (<?php echo date('n/j/y \a\t g:i A', strtotime($version->created_at)); ?>)</option>
			<?php
				$i--;
			endforeach;
			?>
		</select>
	</div>
	
	<?php $last_role = $this->userland->get_role_id($bricabox->id, $WIKI_USERS[$first_ver->user_id]->id); ?>
	<span class="last_editor_role role_<?php echo $last_role; ?>">
		&nbsp;
	</span>
	
	<a class="toggle" style="background-image: url(/assets/images/icons/time.png) !important;" href="#" onclick="$('#wiki_wrap_version_<?php echo $inst->id; ?>').toggle(); return false;">Choose version</a>
	<a class="toggle" href="#" onclick="$('#wiki_wrap_version_<?php echo $inst->id; ?>').hide(); return PrefPane.toggle(<?php echo $inst->id; ?>);">Edit this wiki</a>
	<?php else: ?>
	<a class="toggle" href="#" onclick="return PrefPane.toggle(<?php echo $inst->id; ?>);">Be the first to add to this wiki!</a>
	<?php endif; ?>
	
	<!-- Tiny MCE -->
	<script type="text/javascript" charset="utf-8">
		tinyMCE.init({
			mode: 		"exact",
			elements: 	"wiki_text_<?php echo $inst->id; ?>",
			theme: 		"advanced",
			plugins: 	"safari",
			theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,bullist,numlist,|,link,unlink,image,cleanup,|,help",
			theme_advanced_buttons2: "",
			theme_advanced_buttons3: "",
			theme_advanced_buttons4: "",
			theme_advanced_toolbar_location: "top"
		});
	</script>
	
	<!-- Editor -->
	<div id="block_prefs_<?php echo $inst->id; ?>" style="display: none;">
		<form action="/bricabox/<?php echo $bricabox->id; ?>/save_wiki" method="post">
			<input type="hidden" name="id" value="<?php echo $inst->id; ?>" />
			<input type="hidden" name="return_to" value="/<?php echo $this->uri->uri_string(); ?>" />
			<input type="hidden" name="content_id" value="<?php echo $content->id; ?>" />
			<fieldset>
				<div>
					<label for="wiki_text_<?php echo $inst->id; ?>">Edit this wiki:</label><br />
					<textarea name="value"
					id="wiki_text_<?php echo $inst->id; ?>"
					rows="15" cols="25"
					style="width: 99%; height: 235px;"><?php if ($value) echo htmlentities($value); ?></textarea>
				</div>
				
				<div class="controls">
					<input type="submit" value="Save changes" />
					<?php if ($versions): ?>
					<input type="button" onclick="Wiki.makeDefault(<?php echo $inst->id; ?>, <?php echo $content->id; ?>); $('#block_prefs_<?php echo $inst->id; ?>').hide(); return false;" value="Default to this version" />
					<?php endif; ?>
					or <a href="#" onclick="return PrefPane.toggle(<?php echo $inst->id; ?>);">cancel</a>
				</div>
			</fieldset>
		</form>
	</div>
</div>
<?php endif; ?>

<!-- JS for this instance -->
<script type="text/javascript" charset="utf-8">
	var Wiki = {
		updateWiki: function(inst_id, content_id)
		{
			if (content_id < 0)
				content_id = 1;
			var ver_id = $('#wiki_version_<?php echo $inst->id; ?>').val();
			$('#wiki_text_for_<?php echo $inst->id; ?>').load('/content/'+content_id+'/load_wiki_version/'+ver_id);
		},

		makeDefault: function(inst_id, content_id)
		{
			if (content_id < 0)
				content_id = 1;
			var ver_id = $('#wiki_version_<?php echo $inst->id; ?>').val();
			$.post('/content/'+content_id+'/set_default_wiki_version/'+ver_id);
			return false;
		}
	}
</script>

<?php echo wiki_text_css(); ?>

<?php if (isset($last_role)): ?>
<script type="text/javascript" charset="utf-8">
	$('#wiki_text_for_<?php echo $inst->id; ?>').addClass('role_<?php echo $last_role; ?>');
</script>
<?php endif; ?>